package org.jash.utils.net

import org.jash.utils.log
import retrofit2.Call
import retrofit2.CallAdapter
import retrofit2.Retrofit
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type

class SynchronousCallAdapterFactory :CallAdapter.Factory() {
    override fun get(
        returnType: Type,
        annotations: Array<out Annotation>,
        retrofit: Retrofit
    ): CallAdapter<*, *>? {
        if ((returnType as? ParameterizedType)?.rawType == Call::class.java) {
            return null
        }
        return SynchronousCallAdapter<Any>(returnType)
    }

}
class SynchronousCallAdapter<R>(val type: Type):CallAdapter<R, R> {

    override fun responseType(): Type = type

    override fun adapt(call: Call<R>): R = call.execute().body() ?: throw RuntimeException("SynchronousCallAdapter")

}
